/**
 * Created by Administrator on 2016/10/16.
 */
var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var DateUtil = require('../lib/utils/DateUtil');
var _ = require("underscore");

var connection = require('../lib/utils/DbconnectUtil');

/**
 * 查询项目列表
 */
router.get('/list', function (req, res, next) {
    //console.log("route: /list");
    var querySql = 'select * from t_project where 1=1 ';
    connection().query(querySql, function (error, results) {
        if (error) {
            console.log(error);
            next(error);
        } else {
            res.send(results);
        }
    });
});

/**
 * 查询项目内容
 */
router.get('/content', function (req, res, next) {
    //console.log("route: /content");
    var projectId = req.query.projectId
    //console.log('Router: project/content/' + projectId);
    var querySql = 'select * from t_project_content where project= ?';
    connection().query(querySql,
        [projectId], function (err, results) {
            if (err) {
                console.log(err);
                next(err);
            } else {
                //console.log("getContent successfully. ", results);
                res.send(_.map(results, function (data) {
                    return _.extend(data, {
                        create_at: DateUtil.dateFormat(data.create_at, 'yyyy-MM-dd hh:mm'),
                        update_at: DateUtil.dateFormat(data.update_at, 'yyyy-MM-dd hh:mm')
                    });
                })[0]);
            }
        });
});

/**
 * 更新项目内容
 */
router.put('/updateContent', function (req, res, next) {
    //console.log("route: /updateContent");
    //console.log(req.query);
    connection().query('UPDATE t_project_content SET title = ?, content = ?, status = ?, update_at = ? WHERE project = ?',
        [req.query.title, req.query.content, req.query.status, new Date(), req.query.project], function (err, results) {
            if (err) {
                console.log(err);
                next(err);
            } else {
                connection().query('UPDATE t_project SET name = ? WHERE id = ?',
                    [req.query.title, req.query.project], function (err, results) {
                        if (err) {
                            console.log(err);
                            next(err);
                        } else {
                            console.log('Update Project content successfully. ');
                            res.send(results);
                        }
                    });
            }
        });
});

/**
 * add项目内容
 */
router.put('/addContent', function (req, res, next) {
    //console.log("route: /addContent");
    //console.log(req.query);
    var parent = null;
    var level = 0;
    if (req.query.parent != null) {
        parent = parseInt(req.query.parent);
    }
    if (req.query.level != null) {
        level = parseInt(req.query.level);
    }
    //console.log("before add project 2");
    connection().query('INSERT INTO t_project (name, level, parent) VALUES(?, ?, ?)',
        [req.query.title, level, parent], function (err, results) {
            if (err) {
                console.log(err);
                next(err);
            } else {
                //console.log("init project content: " + results.insertId);
                connection().query('INSERT INTO t_project_content (project, title, content, create_by, create_at, update_by, update_at, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
                    [results.insertId, req.query.title, req.query.content, 'admin', new Date(), 'admin', new Date(), req.query.status ], function (err, ret) {
                        if (err) {
                            console.log(err);
                            next(err);
                        } else {
                            console.log('Add project content successfully. ');
                            res.send({id: results.insertId, name:req.query.name, level: level, parent:parent});
                        }
                    });
            }
        });
});

/**
 * 删除文章
 */
router.delete('/:id', function (req, res, next) {

    if(!req.cookies.jwtToken || !req.cookies.adminAuth){
        res.send({result:false,message:"当前用户未登录",code:403});
    }

    if(!req.cookies.jwtToken || !req.cookies.adminAuth){
        console.log('invalid');
        res.send({result:false,message:"当前用户未登录",code:403});
        return;
    }

    connection().query('DELETE FROM t_project_content WHERE project = ?',
        [req.params.id], function (err, results) {
            if (err) {
                console.log(err);
                next(err);
            } else {
                console.log("Delete project content successfully. ");
                //console.log(results);
                connection().query('DELETE FROM t_project WHERE id = ?',
                    [req.params.id], function (err, results) {
                        if (err) {
                            console.log(err);
                            next(err);
                        } else {
                            console.log("Delete project successfully. ");
                            //console.log(results);
                            res.send(results);
                        }
                    });
            }
        });
});
module.exports = router;